package com.efast.cafe.portal.controller.statemach;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.statemach.PortalStateMachHandle;
import com.efast.cafe.portal.bean.statemach.PortalStateMachHandleQueryBean;
import com.efast.cafe.portal.service.statemach.IPortalStateMachHandleService;

@Controller
@RequestMapping("/statemach")
public class PortalStateMachHandleController extends AbstractController {

	private IPortalStateMachHandleService portalStateMachHandleService;

	@Autowired
	public void setPortalStateMachHandleService(IPortalStateMachHandleService portalStateMachHandleService) {
		this.portalStateMachHandleService = portalStateMachHandleService;
	}

	/**
	 * 企业操作编辑
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/handle/edit")
	public @ResponseBody ResultBean handleEdit(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachHandle obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			obj.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalStateMachHandleService.updateHandle(obj);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;

	}

	/**
	 * 企业操作新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/handle/add")
	public @ResponseBody ResultBean handleAdd(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachHandle obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			obj.setCompanyCode(SpringWebUtils.getRequestCompany());
			obj.setCreateUser(SpringWebUtils.getRequestUser());
			portalStateMachHandleService.insertHandle(obj);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}

	/**
	 * 企业状态机配置删除
	 * 
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/handle/del")
	public @ResponseBody ResultBean handleDel(@RequestBody PortalStateMachHandle obj) {
		ResultBean result = new ResultBean(true, null);
		try {
			obj.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalStateMachHandleService.deleteHandle(obj);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}
	
	/**
	 * 查询状态机配置列表
	 * @param QueryBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/handle/list")
	public @ResponseBody DataTableVo<PortalStateMachHandleQueryBean> queryAllObjectList(@RequestBody PortalStateMachHandleQueryBean QueryBean, HttpServletRequest request) {
		QueryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		return outPageJSONArray_bootstrapDatatable(portalStateMachHandleService.queryPageStateMachHandle(QueryBean));
	}

}
